// pages/orderDetail/orderDetail.js
import config from '../../config.js';
const base = config;
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: null,
    order: null,
    orderStatus: []
  },
  openMap: function(e) {
    let lati = e.currentTarget.dataset.lati,
      long = e.currentTarget.dataset.long,
      address = e.currentTarget.dataset.address;
    console.log(lati, long, address);
    wx.openLocation({
      latitude: lati,
      longitude: long,
      scale: 18,
      address: address,
    })
  },
  contact: function() {
    wx.makePhoneCall({
      phoneNumber: "400-9988-960",
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var id = options.id;
    if (id != null) {
      this.setData({
        orderId: id
      })

      // this.getTraceList();
    }

    // this.loadData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    wx.showLoading({
      title: '数据加载中...',
    })
    this.getTraceList();
    this.loadData();
    this.listenOrderChange();
  },
  listenOrderChange: function() {
    var that = this;
    app.globalData.callback = function(res) {
      console.log('收到服务器推送订单消息内容：' + res.data);
      //解析消息 
      that.loadData();
      that.getTraceList();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  loadData() {
    var that = this;
    //获取订单详情
    wx.showLoading({
      title: '数据正在加载中...',
    })
    wx.request({
      url: base.orderDetailUrl,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        id: this.data.orderId
      },
      method: "POST",
      success: function(res) {
        console.log(res.data);
        if (res.data.status == 200) {
          that.setData({
            order: res.data.data
          })


        } else {
          wx.showToast({
            title: '订单获取失败！',
            icon: 'none',
            duration: 1500
          })
          return false;
        }

        wx.hideLoading();
      }

    })
  },
  getTraceList() {
    var that = this;
    //获取订单详情
    wx.request({
      url: base.orderTraceList,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        id: this.data.orderId
      },
      method: "POST",
      success: function(res) {
        console.log(res.data);
        if (res.data.status == 200) {
          if (res.data.data != null) {
            that.setData({
              orderStatus: res.data.data
            })
          }

        } else {
          wx.showToast({
            title: '订单获取失败！',
            icon: 'none',
            duration: 1500
          })
          return false;
        }
      }

    })
  },
  previewImg: function(e) {
    console.log(e.currentTarget.dataset.url);
    var url = e.currentTarget.dataset.url;
    wx.previewImage({
      current: url, //当前图片地址 
      urls: [url], //所有要预览的图片的地址集合 数组形式 
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  //支付：
  pay: function() {
    var that = this
    var openId = wx.getStorageSync("openID");
    var orderId = that.data.orderId;
    console.log(orderId);
    wx.showLoading({
      title: '发起支付...',
    })
    wx.request({
      url: base.orderPay,
      data: {
        //用户的openid
        openId: openId,
        orderId: orderId, //
      },
      success: function(result) {
        console.log(result);
        wx.hideLoading();
        if (result.data.status == 200) {
          //out_trade_no=res.data['out_trade_no'];
          wx.requestPayment({
            timeStamp: result.data.timeStamp,
            nonceStr: result.data.nonceStr,
            package: result.data.package,
            signType: 'MD5',
            paySign: result.data.paySign,
            success: function(successret) {
              console.log('支付成功');
              //获取支付用户的信息
              wx.showLoading({
                title: '支付成功！',
              })
              setTimeout(function() {
                that.payResult(orderId, result.data.orderNo);
                wx.hideLoading();
              }, 300);

            },
            fail: function(res) {
              console.log(res);

              wx.showToast({
                title: '支付取消！',
                icon: 'none',
                duration: 1000
              })

            }
          })
        } else {
          console.log();
        }
      }
    })
  },
  payResult: function(id, No) {
    var that = this;
    wx.showLoading({
      title: '更新订单...',
    })
    wx.request({
      url: base.orderPayResult,
      data: {
        //用户的openid
        orderId: id,
        orderNo: No,
        type: 1
      },
      success: function(result) {
        console.log(result);
        wx.hideLoading();
        if (result.data.status == 200) {
          wx.showLoading({
            title: '订金支付成功',
          })
          that.loadData();
          setTimeout(function() {
            wx.hideLoading();

          }, 300);

        } else if (result.data.status == 201) {
          //订单更新失败；
          //记录当前订单OrderId;
          wx.setStorageSync("payError", id);
        }
      }
    })
  },
  payAction: function(e) {
    wx.navigateTo({
      url: './paymentItems?orderId=' + this.data.orderId + "&typeId=" + this.data.order.order.orderType.typeId,
    })
  }
})